<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\SchoolLikeUsers;
use App\Models\SchoolRoles;
use App\Models\SchoolRoleUsers;
use App\Models\User;
use Illuminate\Support\Facades\Auth;

class TeacherController extends Controller
{
    /**
     * 获取学生列表
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function studentGrid()
    {
        $teacherId = Auth::id();
        $schoolIds = SchoolRoleUsers::where('user_id', $teacherId)
            ->whereIn('role_id', [SchoolRoles::MANAGER_ROLE, SchoolRoles::TEACHER_ROLE])
            ->pluck('school_id');
        $studentIds = SchoolRoleUsers::whereIn("school_id", $schoolIds)
            ->where('role_id', SchoolRoles::STUDENT_ROLE)
            ->pluck("user_id");
        $user = User::with('school')
            ->whereIn("id", $studentIds)
            ->get();
        $user = $user->map(function($item){
            $item->avatar = '';
            $item->student_school = $item->school->first();
            return $item;
        });
        return success($user);
    }

    /**
     * 获取关注自己的学生列表
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function likeGrid()
    {
        $teacherId = Auth::id();
        $studentIds = SchoolLikeUsers::where('teacher_id', $teacherId)
            ->pluck("student_id");
        $user = User::with('school')
            ->whereIn("id", $studentIds)
            ->get();

        return success($user);
    }
}